package com.wanpinghui.supplier.user;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.common.BaseFragment;
import com.wanpinghui.supplier.oauth.LoginUtil;
import com.wanpinghui.supplier.user.order.OrderActivity;
import com.wanpinghui.supplier.user.product.ProductManagerActivity;
import com.wanpinghui.wphlibrary.WPHSdk;
import com.wanpinghui.wphlibrary.model.oauth.OAuthService;
import com.wanpinghui.wphlibrary.model.oauth.bean.UserBean;
import com.wanpinghui.wphlibrary.model.oauth.bean.UserInformation;
import com.wanpinghui.wphlibrary.model.oauth.bean.UserSetingBean;
import com.yunshl.yunshllibrary.utils.DevicesUtil;
import com.yunshl.yunshllibrary.utils.TextUtil;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import jp.wasabeef.glide.transformations.CropCircleTransformation;
import retrofit.callback.YRequestCallback;

/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2017/11/23
 ***/
@ContentView(R.layout.layout_fragment_mine2)
public class MineFragment extends BaseFragment {
    @ViewInject(R.id.iv_head)
    private ImageView ivHead;
    @ViewInject(R.id.tv_name)
    private TextView tvName;
    @ViewInject(R.id.tv_company_name)
    private TextView tvCompanyName;
    @ViewInject(R.id.rl_mine_package)
    private RelativeLayout rlMinePackage;
    @ViewInject(R.id.v_package_cricle)
    private View vPackageCricle;
    @ViewInject(R.id.tv_mine_package_content)
    private TextView tvMinePackageContent;
    @ViewInject(R.id.rl_mine_scope)
    private RelativeLayout rlMineScope;
    @ViewInject(R.id.v_scope_cricle)
    private View vScopeCricle;
    @ViewInject(R.id.tv_mine_scope_content)
    private TextView tvMineScopeContent;
    @ViewInject(R.id.rl_mine_level)
    private RelativeLayout rlMineLevel;
    @ViewInject(R.id.tv_mine_level_content)
    private TextView tvMineLevelContent;
    @ViewInject(R.id.rl_mine_setting)
    private RelativeLayout rlMineSetting;
    @ViewInject(R.id.tv_mine_setting_content)
    private TextView tvMineSettingContent;
    @ViewInject(R.id.rl_userinfo)
    private RelativeLayout rlUserinfo;
    @ViewInject(R.id.iv_active)
    private ImageView iv_active;
    @ViewInject(R.id.iv_level)
    private ImageView iv_level;
    @ViewInject(R.id.ll_manager)
    private LinearLayout mLayoutManager;
    @ViewInject(R.id.rl_product_manager)
    private RelativeLayout layoutProductManager;

    @ViewInject(R.id.rl_order_manager)
    private RelativeLayout rl_order_manager;

    @ViewInject(R.id.tv_phone)
    private TextView tv_phone;

    @ViewInject(R.id.tv_address)
    private TextView tv_address;

    private boolean haveStop;
    private UserBean userBean;

    @Override
    public void init() {
//        if (Config.VERSION == 1) {
//            mLayoutManager.setVisibility(View.GONE);
//        }else {
//            mLayoutManager.setVisibility(View.VISIBLE);
//        }
        bindEvents();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            if (LoginUtil.isLogin()) {
                getUserInformation();
            }
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        haveStop = true;
    }

    @Override
    public void onStart() {
        super.onStart();
        if (haveStop && LoginUtil.isLogin()) {
            getUserInformation();
        }
    }

    private void getUserInformation() {
        userBean = WPHSdk.getService(OAuthService.class)
                .getUserInfo(new YRequestCallback<UserBean>() {
                    @Override
                    public void onSuccess(UserBean userBeanInfo) {
                        showInfo(userBeanInfo);

                    }

                    @Override
                    public void onFailed(int var1, String message) {
                        //   showWarningText("登录失败，获取用户信息失败");
                        showInfo(userBean);
                    }

                    @Override
                    public void onException(Throwable var1) {
                        var1.printStackTrace();
                    }
                });

    }

    public void showInfo(UserBean userBeanInfo) {
        if (userBeanInfo != null && userBeanInfo.getUser() != null) {
            UserInformation userBean = userBeanInfo.getUser();
            if (TextUtil.isEmpty(userBean.getCom()) && getActivity() != null) {
                Drawable drawable = ContextCompat.getDrawable(getActivity(),R.mipmap.address_list_friend_warn);
                drawable.setBounds(0, 0, 40, 40);
                tvCompanyName.setCompoundDrawablePadding(DevicesUtil.dip2px(getActivity(),5f));
                tvCompanyName.setCompoundDrawables(drawable,null,null,null);
                tvCompanyName.setText("编辑您的公司信息/职称");
//                if (TextUtil.isNotEmpty(userBean.getPos())) {
//                    tvCompanyName.setText("个体供应商 | " + userBean.getPos());
//                } else {
//                    tvCompanyName.setText("个体供应商  | 暂无");
//                }
            } else {
                tvCompanyName.setCompoundDrawables(null,null,null,null);
                if (TextUtil.isNotEmpty(userBean.getPos())) {
                    tvCompanyName.setText(userBean.getCom()+ " | " + userBean.getPos());
                } else {
                    tvCompanyName.setText(userBean.getCom()+ " | 暂无");
                }

            }
            tv_phone.setText(userBean.getMobile());
            if(TextUtil.isNotEmpty(userBean.getCity())){
                tv_address.setText(userBean.getCity());
            }
            else if (TextUtil.isNotEmpty(userBean.getAddr())){
                tv_address.setText(userBean.getAddr());
            }else {
                tv_address.setText(userBean.getPhone_area_name());
            }
            tvName.setText(userBean.getNickname());
            Glide.with(getActivity()).load(userBean.getAvatar() + "_thumbnail").thumbnail(0.3f).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.RESULT).placeholder(R.mipmap.address_list_bg_head).bitmapTransform(new CropCircleTransformation(getActivity())).into(ivHead);
            if (userBean.getLevel() <= 10) {
                tvMineLevelContent.setText("铁牌");
            } else {
                tvMineLevelContent.setText(userBean.getLevel_display());
            }

            if (userBean.isHas_set_business()) {
                tvMineScopeContent.setText("服务类型、区域");
                vScopeCricle.setVisibility(View.GONE);
            } else {
                tvMineScopeContent.setText("未设置");
                vScopeCricle.setVisibility(View.VISIBLE);
            }
            if (userBean.isPass()) {
                iv_active.setVisibility(View.VISIBLE);
            } else {
                iv_active.setVisibility(View.GONE);
            }
            switch (userBean.getLevel()) {
                case 10:
                    iv_level.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.mipmap.my_icon_member_1));
                    break;
                case 20:
                    iv_level.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.mipmap.my_icon_member_2));
                    break;
                case 30:
                    iv_level.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.mipmap.my_icon_member_3));
                    break;
                case 40:
                    iv_level.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.mipmap.my_icon_member_4));
                    break;
                default:
                    iv_level.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.mipmap.my_icon_member_1));
                    break;

            }
            if (userBean.isHas_set_business()) {
                tvMineScopeContent.setText("服务类型、区域");
                vScopeCricle.setVisibility(View.GONE);
            } else {
                tvMineScopeContent.setText("未设置");
                vScopeCricle.setVisibility(View.VISIBLE);
            }
        }
    }

    private void bindEvents() {
        rlMineLevel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), MemberLevelActivity.class));
            }
        });

        rlMinePackage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), MinePackageActivity2.class));
            }
        });

        rlMineScope.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), BusinessScopeActivity.class));
            }
        });

        rlMineSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), SettingActivity.class));
//                Network.getApi(IWPHApi.class).get("13929569170","13580293793").subscribeOn(Schedulers.io())
//                        .observeOn(AndroidSchedulers.mainThread())
//                        .subscribe(new Action1<YunShlResult<Object>>() {
//                            @Override
//                            public void call(YunShlResult<Object> objectYunShlResult) {
//
//                            }
//                        },new YunShlAction<Throwable>(null));
            }
        });

        rlUserinfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), UpdateUserInfoActivity.class));
            }
        });
        rl_order_manager.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), OrderActivity.class));
            }
        });

        layoutProductManager.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), ProductManagerActivity.class));
            }
        });

    }


}
